<?php

declare(strict_types=1);

namespace app\system\controller;

use app\admin\BaseAdminController;
use think\facade\View;
use app\system\model\Dept as DeptModel;
use app\system\model\LogOperate as Log;
use app\admin\model\Admin;
use think\db\Query;

class Dept extends BaseAdminController
{
    /**
     * 部门管理页面
     */
    public function index()
    {
        View::assign('title', '部门管理/系统管理');
        View::assign('tree', DeptModel::getOptions());
        View::assign('users', Admin::where('status', 0)->column('nickname', 'id'));
        View::assign('addJs', addJs(['treegrid']));
        return View::fetch();
    }

    /**
     * 读取部门信息列表
     */
    public function data()
    {
        $list = DeptModel::with([
            'user' => function (Query $query) {
                $query->withTrashed();
                $query->field('id,nickname');
            }
        ])->order(['sort' => 'asc', 'id' => 'desc'])->select();
        return json($list);
    }

    /**
     * 保存部门信息
     */
    public function save()
    {
        $id = input('id/d', 0);
        $pid = input('pid/d', 0);
        $name = input('name');
        $sort = input('sort/d', 0);
        $admin_id = input('admin_id/d', 0);
        if ($id) {
            $title = '编辑部门';
            $obj = DeptModel::find($id);
        } else {
            $title = '新增部门';
            $obj = new DeptModel;
        }
        $icon = input('icon', '');
        $image = input('image', '');
        $obj->pid = $pid;
        $obj->name = $name;
        $obj->sort = $sort;
        $obj->admin_id = $admin_id;
        $obj->update_by = USERNAME;
        $obj->icon = $image;
        $obj->image = $icon;
        $obj->save();
        $admin = Admin::find($admin_id);
        Log::add($title, [
            'ID' => $obj->id,
            '部门名称' => $name,
            '负责人' => $admin ? $admin->nickname : '-',
            '部门图标' => $icon,
            '字体图标' => $image,
            '显示顺序' => $sort,
        ]);
        return json(['msg' => '']);
    }

    /**
     * 部门状态变更
     */
    public function status()
    {
        $id = input('id/d', 0);
        $obj = DeptModel::find($id);
        $status = $obj->status ? '正常' : '停用';
        $obj->status = $obj->status ? 0 : 1;
        $obj->update_by = USERNAME;
        $obj->save();
        Log::add('部门状态', [
            'ID' => $id,
            '部门名称' => $obj->name,
            '状态' => $status,
        ]);
        return json(['msg' => '']);
    }

    /**
     * 删除部门信息
     */
    public function remove()
    {
        $id = input('id/d', 0);
        $count = DeptModel::where('pid', $id)->count();
        if ($count) {
            return json(['msg' => '该部门下存在子部门']);
        }
        $obj = DeptModel::find($id);
        $obj->delete();
        Log::add('删除部门', [
            'ID' => $id,
            '部门名称' => $obj->name,
            '显示顺序' => $obj->sort,
        ]);
        return json(['msg' => '']);
    }

    /**
     * 查看部门信息
     */
    public function view()
    {
        $id = input('id/d', 0);
        $obj = DeptModel::find($id);
        return json($obj);
    }
}
